package com.nananren.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nananren.anno.OperateLogLog;
import com.nananren.common.Result;
import com.nananren.domain.Book;
import com.nananren.domain.Borrow;
import com.nananren.domain.User;
import com.nananren.dto.BorrowDto;
import com.nananren.service.BookService;
import com.nananren.service.BorrowService;
import com.nananren.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("borrow")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    // 借阅图书(insert)
    @PostMapping
    @OperateLogLog
    public Result save(@RequestBody Borrow borrow) {
        log.info("借阅图书，借阅信息为：{}", borrow.toString());
        // 判断bookId是否存在
        LambdaQueryWrapper<Book> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookLambdaQueryWrapper.eq(Book::getIsDeleted, 1);
        bookLambdaQueryWrapper.eq(Book::getId, borrow.getBookId());
        Book book = bookService.getOne(bookLambdaQueryWrapper);
        if (book == null) {
            return Result.error("当前图书不存在!!");
        }

        // 判断userId是否存在
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getIsDeleted, 1);
        userLambdaQueryWrapper.eq(User::getId, borrow.getUserId());
        User user = userService.getOne(userLambdaQueryWrapper);
        if (user == null) {
            return Result.error("当前用户不存在!!");
        }

        // 判断number是否大于book表中的quantity数量
        if (borrow.getNumber() != null) {
            Book bookWithQuantity = bookService.getById(borrow.getBookId());
            if (borrow.getNumber() > bookWithQuantity.getQuantity()) {
                return Result.error("借阅数量超过了库存数量!!");
            }
        }

        // 图书表的quantity -= burrow.getNumber()
        Book bookInDB = bookService.getById(borrow.getBookId());
        bookInDB.setQuantity(bookInDB.getQuantity() - borrow.getNumber());
        bookService.updateById(bookInDB);

        // 正常插入
        borrowService.save(borrow);
        return Result.success("借阅成功!!");
    }

    // 归还图书(update)
    @PutMapping
    @OperateLogLog
    public Result updateById(@RequestBody Borrow borrow) {
        log.info("归还图书，图书信息为{}", borrow.toString());

        // 传递的数据不完整
        if (borrow.getId() == null || borrow.getUserId() == null || borrow.getBookId() == null) {
            return Result.error("归还数据有误!!");
        }

        // 根据借阅id、用户id、图书id和is_deleted字段为1判断该用户是否借阅了该图书
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getId, borrow.getId());
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, borrow.getUserId());
        borrowLambdaQueryWrapper.eq(Borrow::getBookId, borrow.getBookId());
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 1);
        Borrow borrowInDB = borrowService.getOne(borrowLambdaQueryWrapper);
        if (borrowInDB == null) {
            return Result.error("暂无借阅记录");
        }

        // 归还(update)：将back_time和update_time更新为当前时间，并且将status字段设置为0(逻辑删除)
        borrow.setBackTime(LocalDateTime.now());
        borrow.setUpdateTime(LocalDateTime.now());
        borrow.setStatus(0);

        // 将图书表的quantity字段恢复
        Book book = bookService.getById(borrow.getBookId());
        book.setQuantity(book.getQuantity() + borrowInDB.getNumber());
        bookService.updateById(book);

        // 归还
        borrowService.updateById(borrow);

        return Result.success("归还成功!");
    }

    // 查询所有借阅信息(select)
    @GetMapping("/list")
    public Result list() {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list != null ? Result.success(list) : Result.error("暂无数据");
    }

    // 查询所有已删除的借阅信息
    @GetMapping("/isDeleted/0")
    public Result isDeleted0() {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 0);
        // 根据更新时间倒序排序
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 查询所有未删除的借阅信息
    @GetMapping("/isDeleted/1")
    public Result isDeleted1() {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // 根据更新时间倒序排序
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 查询所有已归还的借阅信息
    @GetMapping("/status/0")
    public Result status0() {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 0);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 查询所有未归还的借阅信息
    @GetMapping("/status/1")
    public Result status1() {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 1);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 根据用户id查询其所有借阅信息
    @GetMapping("/{userId}")
    public Result getById(@PathVariable Long userId) {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 根据用户id查询所有已删除的借阅信息
    @GetMapping("/isDeleted/0/{userId}")
    public Result getByIdWithIsDeleted0(@PathVariable Long userId) {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 0);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 根据用户id查询所有未删除的借阅信息
    @GetMapping("/isDeleted/1/{userId}")
    public Result getByIdWithIsDeleted1(@PathVariable Long userId) {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 根据用户id查询其所有已归还的借阅信息
    @GetMapping("/status/0/{userId}")
    public Result getByIdWithStatus0(@PathVariable Long userId) {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 0);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 根据用户id查询其所有未归还的借阅信息
    @GetMapping("/status/1/{userId}")
    public Result getByIdWithStatus1(@PathVariable Long userId) {
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 1);
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        return list.size() != 0 ? Result.success(list) : Result.error("暂无数据");
    }

    // 分页条件查询所有借阅信息(借阅时间)
    @GetMapping("/page")
    public Result list(Integer page, Integer pageSize,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("分页查询所有借阅信息：{}, {}, {}, {}", page, pageSize, begin, end);
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // begin等于null，end不等于null，查询borrow_time小于等于end的所有数据
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不等于null，end等于null，查询borrow_time大于等于begin的所有数据
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不等于null，查询borrow_time大于等于begin并小于等于end的所有数据
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 条件分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);
        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");
    }

    // 分页条件查询所有已删除借阅信息
    @GetMapping("/page/isDeleted/0")
    public Result isDeleted0(Integer page, Integer pageSize,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("分页条件查询所有已删除借阅信息：{}, {}, {}, {}", page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 所有已删除借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 0);
        // begin为null，end不为null
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为null，end为null
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为null
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 根据borrow_time倒序排序
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        // 分页条件查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);
        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");

    }

    // 分页条件查询所有未删除借阅信息
    @GetMapping("/page/isDeleted/1")
    public Result isDeleted1(Integer page, Integer pageSize,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("分页条件查询所有已删除借阅信息：{}, {}, {}, {}", page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 所有未删除借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // begin为null，end不为null
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为null，end为null
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为null
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 根据borrow_time倒序排序
        borrowLambdaQueryWrapper.orderByDesc(Borrow::getUpdateTime);
        // 分页条件查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);
        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");

    }

    // 分页条件查询所有已归还的借阅信息
    @GetMapping("/page/status/0")
    public Result status0(Integer page, Integer pageSize, String bookName, String username, String number,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end){
        log.info("分页条件查询所有已归还的借阅信息：{}, {}, {}, {}, {}, {}, {}", page, pageSize, bookName, username, number, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        Page<BorrowDto> borrowDtoPage = new Page<>();
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 已归还
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 0);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // 图书名称bookName
        if (bookName != null) {
            LambdaQueryWrapper<Book> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
            bookLambdaQueryWrapper.like(Book::getName, bookName);
            List<Book> list = bookService.list(bookLambdaQueryWrapper);
            if (list.size() == 0) {
                return Result.error("暂无数据");
            }
            // 得到list里面的每一个id
            List<Long> ids = list.stream().map(Book::getId).collect(Collectors.toList());
            borrowLambdaQueryWrapper.in(Borrow::getBookId, ids);
        }

        // 用户名userName
        if (username != null) {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.like(User::getUsername, username);
            List<User> list = userService.list(userLambdaQueryWrapper);
            if (list.size() == 0) {
                return Result.error("暂无数据");
            }
            List<Long> ids = list.stream().map(User::getId).collect(Collectors.toList());
            borrowLambdaQueryWrapper.in(Borrow::getUserId, ids);
        }

        // 数量number
        borrowLambdaQueryWrapper.eq(number != null && number != "", Borrow::getNumber, number);
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(borrowPage, borrowDtoPage, "records");
        List<Borrow> records = borrowPage.getRecords();
        List<BorrowDto> result = records.stream().map(item -> {
            BorrowDto borrowDto = new BorrowDto();
            //对象拷贝
            BeanUtils.copyProperties(item, borrowDto);
            // 图书名称bookName
            Long bookId = item.getBookId();
            Book book = bookService.getById(bookId);
            if (book != null) {
                borrowDto.setBookName(book.getName());
            }
            // 借阅者username
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            if (user != null) {
                borrowDto.setUsername(user.getUsername());
            }
            return borrowDto;
        }).collect(Collectors.toList());

        borrowDtoPage.setRecords(result);

        return borrowDtoPage.getTotal() != 0 ? Result.success(borrowDtoPage) : Result.error("暂无数据");
    }

    // 分页条件查询所有未归还的借阅信息
    @GetMapping("/page/status/1")
    public Result status1(Integer page, Integer pageSize, String bookName, String username, String number,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end){
        log.info("分页条件查询所有已归还的借阅信息：{}, {}, {}, {}, {}, {}, {}", page, pageSize, bookName, username, number, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        Page<BorrowDto> borrowDtoPage = new Page<>();
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 已归还
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 1);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // 图书名称bookName
        if (bookName != null) {
            LambdaQueryWrapper<Book> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
            bookLambdaQueryWrapper.like(Book::getName, bookName);
            List<Book> list = bookService.list(bookLambdaQueryWrapper);
            if (list.size() == 0) {
                return Result.error("暂无数据");
            }
            // 得到list里面的每一个id
            List<Long> ids = list.stream().map(Book::getId).collect(Collectors.toList());
            borrowLambdaQueryWrapper.in(Borrow::getBookId, ids);
        }

        // 用户名userName
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.like(username != null, User::getUsername, username);
            List<User> list = userService.list(userLambdaQueryWrapper);
            if (list.size() == 0) {
                return Result.error("暂无数据");
            }
            List<Long> ids = list.stream().map(User::getId).collect(Collectors.toList());
            borrowLambdaQueryWrapper.in(Borrow::getUserId, ids);

        // 数量number
        borrowLambdaQueryWrapper.eq(number != null && number != "", Borrow::getNumber, number);
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(borrowPage, borrowDtoPage, "records");
        List<Borrow> records = borrowPage.getRecords();
        List<BorrowDto> result = records.stream().map(item -> {
            BorrowDto borrowDto = new BorrowDto();
            //对象拷贝
            BeanUtils.copyProperties(item, borrowDto);
            // 图书名称bookName
            Long bookId = item.getBookId();
            Book book = bookService.getById(bookId);
            if (book != null) {
                borrowDto.setBookName(book.getName());
            }
            // 借阅者username
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            if (user != null) {
                borrowDto.setUsername(user.getUsername());
            }
            return borrowDto;
        }).collect(Collectors.toList());

        borrowDtoPage.setRecords(result);

        return borrowDtoPage.getTotal() != 0 ? Result.success(borrowDtoPage) : Result.error("暂无数据");
    }

    // 根据用户id分页条件查询其所有借阅信息
    @GetMapping("/page/{userId}")
    public Result pageWithId(@PathVariable Long userId, Integer page, Integer pageSize,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("根据用户id分页条件查询其所有借阅信息:{}, {}, {}, {}, {}", userId, page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 查询当前用户的所有借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");
    }

    // 根据用户id分页条件查询其所有已删除借阅信息
    @GetMapping("/page/isDeleted/0/{userId}")
    public Result pageWithIsDeleted0(@PathVariable Long userId, Integer page, Integer pageSize,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("根据用户id分页条件查询其所有借阅信息:{}, {}, {}, {}, {}", userId, page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 查询当前用户的所有已删除借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 0);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");
    }

    // 根据用户id分页条件查询其所有未删除借阅信息
    @GetMapping("/page/isDeleted/1/{userId}")
    public Result pageWithIsDeleted1(@PathVariable Long userId, Integer page, Integer pageSize,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("根据用户id分页条件查询其所有借阅信息:{}, {}, {}, {}, {}", userId, page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 查询当前用户的所有未删除借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");
    }

    // 根据用户id分页条件查询所有已归还的借阅信息
    @GetMapping("/page/status/0/{userId}")
    public Result pageWithStatus0(@PathVariable Long userId, Integer page, Integer pageSize,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("根据用户id分页条件查询其所有借阅信息:{}, {}, {}, {}, {}", userId, page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 查询当前用户的所有未删除借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // 查询所有已归还的借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 0);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");
    }

    // 根据用户id分页条件查询所有未归还的借阅信息
    @GetMapping("/page/status/1/{userId}")
    public Result pageWithStatus1(@PathVariable Long userId, Integer page, Integer pageSize,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        log.info("根据用户id分页条件查询其所有借阅信息:{}, {}, {}, {}, {}", userId, page, pageSize, begin, end);
        // 分页构造器
        Page<Borrow> borrowPage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // begin为空，end不为空
        borrowLambdaQueryWrapper.le(begin == null && end != null, Borrow::getBorrowTime, end);
        // begin不为空，end为空
        borrowLambdaQueryWrapper.ge(begin != null && end == null, Borrow::getBorrowTime, begin);
        // begin和end都不为空
        borrowLambdaQueryWrapper.between(begin != null && end != null, Borrow::getBorrowTime, begin, end);
        // 查询当前用户的所有未删除借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getUserId, userId);
        borrowLambdaQueryWrapper.eq(Borrow::getIsDeleted, 1);
        // 查询所有已归还的借阅信息
        borrowLambdaQueryWrapper.eq(Borrow::getStatus, 1);
        // 分页查询
        borrowService.page(borrowPage, borrowLambdaQueryWrapper);

        return borrowPage.getTotal() != 0 ? Result.success(borrowPage) : Result.error("暂无数据");
    }

    // 根据ids批量删除借阅记录
    @DeleteMapping
    @OperateLogLog
    public Result deleteByIds(@RequestParam("ids") List<Long> ids) {
        log.info("根据ids批量删除借阅记录：{}", ids.toString());
        // 条件构造器
        LambdaQueryWrapper<Borrow> borrowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 判断id是否全都合法
        borrowLambdaQueryWrapper.in(Borrow::getId, ids);
        List<Borrow> list = borrowService.list(borrowLambdaQueryWrapper);
        if (list.size() != ids.size()) {
            // 长度不同，说明传递的id有问题
            return Result.error("当前借阅记录不存在");
        }
        // 数据都合法
        // 利用stream流遍历list里面的每一个数据，将isDeleted修改为0
        list = list.stream().map(item -> {
            item.setIsDeleted(0);
            return item;
        }).collect(Collectors.toList());

        // 批量修改isDeleted的值为0
        borrowService.updateBatchById(list);

        return Result.success("删除成功");
    }

    // 根据id删除借阅信息
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id) {
        log.info("根据id删除借阅记录，id为{}", id);
        Borrow borrow = borrowService.getById(id);
        if (borrow == null) {
            return Result.error("借阅信息不存在");
        }
        borrow.setIsDeleted(0);
        borrowService.updateById(borrow);
        return Result.success("删除成功");
    }

}
